package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.GeRen_Main_Fans_ListView_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class GeRen_Main_Fans_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private String access_token;
    private TextView tv_geren_main_fans_guanzhu, tv_geren_main_fans;
    private ImageView tv_feedback_tijiao;
    private Button btn_gerentg_tgjl, btn_gerentg_ddjl;
    private MyListView lv_xyf_jilu;
    private int uid, fans, watch, type, care;
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_main_fans);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        uid = intent.getIntExtra("uid", 0);
        fans = intent.getIntExtra("fans", 0);
        watch = intent.getIntExtra("watch", 0);
        type = intent.getIntExtra("type", 0);
        care = intent.getIntExtra("care", 0);
        back = findViewById(R.id.back);
        tv_geren_main_fans_guanzhu = findViewById(R.id.tv_geren_main_fans_guanzhu);
        tv_geren_main_fans = findViewById(R.id.tv_geren_main_fans);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        btn_gerentg_tgjl = findViewById(R.id.btn_gerentg_tgjl);
        btn_gerentg_ddjl = findViewById(R.id.btn_gerentg_ddjl);
        lv_xyf_jilu = findViewById(R.id.lv_xyf_jilu);
        back.setOnClickListener(this);
        btn_gerentg_tgjl.setOnClickListener(this);
        btn_gerentg_ddjl.setOnClickListener(this);
        tv_feedback_tijiao.setOnClickListener(this);
        tv_geren_main_fans_guanzhu.setText("" + watch);
        tv_geren_main_fans.setText("" + fans);
        if (care == 1){
            btn_gerentg_tgjl.setText("我的关注");
            btn_gerentg_ddjl.setText("我的粉丝");
        } else {
            btn_gerentg_tgjl.setText("他的关注");
            btn_gerentg_ddjl.setText("他的粉丝");
        }
        if (type == 1){
            btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_exit);
            btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_login);
        } else {
            btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_login);
            btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_exit);
        }
        postData(type);// 1 粉丝 2 关注 3 悄悄关注
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_gerentg_tgjl:
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_login);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_exit);
                postData(type);
                break;
            case R.id.btn_gerentg_ddjl:
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_login);
                postData(type);
                break;
            case R.id.tv_feedback_tijiao:
                Intent intent = new Intent(this, GeRen_Main_Fans_Quiet_Activity.class);
                intent.putExtra("access_token",access_token);
                intent.putExtra("uid", uid);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(int mtype) {
        Log.d("main-list", uid + "");
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.lt_fans_list_Url + access_token + "&page=1&limit=20&uid=" + uid + "&type=" + mtype, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("main_list", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        List<User_Limit> user_limit = gson.fromJson(info.toString(), new TypeToken<List<User_Limit>>(){}.getType());
                        lv_xyf_jilu.setAdapter(new GeRen_Main_Fans_ListView_Adapter(GeRen_Main_Fans_Activity.this, user_limit, 1, 0));
                    } else {
                        ToolUtils.showToast(GeRen_Main_Fans_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRen_Main_Fans_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
